package com.ld.contributor;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.util.ProcessingContext;
import com.ld.tool.window.TranslateMainWindow;
import com.ld.tool.window.TranslatePanel;
import com.ld.translate.TranslateFactory;
import com.ld.utils.CommonUtils;
import org.jetbrains.annotations.NotNull;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyCompletionContributor extends CompletionContributor {

    private static volatile MyMouseListener myLookupListener = null;

    public MyCompletionContributor(){
        extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters parameters,
                                          @NotNull ProcessingContext processingContext,
                                          @NotNull CompletionResultSet result) {
                Editor editor = parameters.getEditor();
                LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
                if (lookup != null) {
                    if (myLookupListener != null){
                        lookup.getList().removeMouseListener(myLookupListener);
                    }
                    myLookupListener = new MyMouseListener(lookup,editor.getProject());
                    lookup.getList().addMouseListener(myLookupListener);
                }
            }});
    }
}
class MyMouseListener implements MouseListener{

    LookupImpl lookup;
    Project project;


    public MyMouseListener(LookupImpl lookup,Project project){
        this.lookup = lookup;
        this.project = project;
    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        LookupElement selectedText = (LookupElement)lookup.getList().getSelectedValue();
        TranslateMainWindow.showToolWindow(project);
        ApplicationManager.getApplication().executeOnPooledThread(()->{
            TranslatePanel translatePanel = null;
            for (int i=0;i<3;i++){
                translatePanel = TranslateMainWindow.getTranslatePanel(project);
                if (translatePanel == null){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }else {
                    break;
                }
            }
            if (translatePanel != null){
                translatePanel.context.setText(TranslateFactory.getInstance().tokens(selectedText.getLookupString()));
//                translatePanel.startTranslate();
            }
        });
    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {

    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {

    }
}
